function sortByCreatedTime(a, b){
	var a_time = a.createdateNum;
	var a_year = a_time.substring(0,4);
	var a_month = a_time.substring(5, 7);
	var a_day = a_time.substring(8, 10);
	var a_hour = a_time.substring(11, 13);
	var a_min = a_time.substring(14, 16);
	var a_sec = a_time.substring(17, 19);
	
	//Original format is in 2011-11-28 15:19:17
	var a_sum = a_year+a_month+a_day+a_hour+a_min+a_sec;
	
	var b_time = b.createdateNum;
	var b_year = b_time.substring(0,4);
	var b_month = b_time.substring(5, 7);
	var b_day = b_time.substring(8, 10);
	var b_hour = b_time.substring(11, 13);
	var b_min = b_time.substring(14, 16);
	var b_sec = b_time.substring(17, 19);
	var b_sum = b_year+b_month+b_day+b_hour+b_min+b_sec;
	
	return a_sum - b_sum;	
}

function sortByUpdated(a, b){
	var a_time = a.timestampNum;
	var a_year = a_time.substring(0,4);
	var a_month = a_time.substring(5, 7);
	var a_day = a_time.substring(8, 10);
	var a_hour = a_time.substring(11, 13);
	var a_min = a_time.substring(14, 16);
	var a_sec = a_time.substring(17, 19);
	
	var a_sum = a_year+a_month+a_day+a_hour+a_min+a_sec;
	
	var b_time = b.timestampNum;
	var b_year = b_time.substring(0,4);
	var b_month = b_time.substring(5, 7);
	var b_day = b_time.substring(8, 10);
	var b_hour = b_time.substring(11, 13);
	var b_min = b_time.substring(14, 16);
	var b_sec = b_time.substring(17, 19);
	var b_sum = b_year+b_month+b_day+b_hour+b_min+b_sec;
	
	return a_sum - b_sum;	
}

function sortByCommented(a, b){
	var a_time = a.lastCommentTimestampNum;
	var a_year = a_time.substring(0,4);
	var a_month = a_time.substring(5, 7);
	var a_day = a_time.substring(8, 10);
	var a_hour = a_time.substring(11, 13);
	var a_min = a_time.substring(14, 16);
	var a_sec = a_time.substring(17, 19);
	
	var a_sum = a_year+a_month+a_day+a_hour+a_min+a_sec;
	//2011-11-28 15:19:17
	
	var b_time = b.lastCommentTimestampNum;
	var b_year = b_time.substring(0,4);
	var b_month = b_time.substring(5, 7);
	var b_day = b_time.substring(8, 10);
	var b_hour = b_time.substring(11, 13);
	var b_min = b_time.substring(14, 16);
	var b_sec = b_time.substring(17, 19);
	var b_sum = b_year+b_month+b_day+b_hour+b_min+b_sec;
	
	return a_sum - b_sum;
}


var sortVal;
function storySortInit() {
	var selectd = document.getElementById("sortBrowse");
	sortVal = selectd.options[selectd.selectedIndex].value;
	sortResults();
	window.setInterval(sortResults, 1000 );
}

function updateSortValue(value) {
	sortVal = value;
}

function sortResults(){
		$.getJSON("/writeonproject/update?page=browse_stories",sortResultsCallBack);
}

function sortResultsCallBack(data){
			if( data.error != null && data.error == "false" ) {
				if( data.stories.length > 0 ) {
					
					if (sortVal == "updated"){
						data.stories.sort(sortByUpdated);			
					}
					
					else if (sortVal == "viewed"){
						data.stories.sort(function(a, b){
							return a.viewcount - b.viewcount;});
					}
					
					else if (sortVal == "voted"){
						data.stories.sort(function(a, b){
							return a.votecount - b.votecount;});		
					}
					
					else if (sortVal == "created"){
						data.stories.sort(sortByCreatedTime);
					}
					
					else if (sortVal == "commented"){
						data.stories.sort(sortByCommented);			
					}
					
					var storybody = document.getElementById('storybody');
					
					for (var i = storybody.rows.length; i > 0; i--){
						storybody.deleteRow(0);
					}
					
					if( data.stories.length > 0 ) {
						
						var tagCheckboxes = document.getElementById('tagsCheckboxes');
						var boxes = tagCheckboxes.getElementsByTagName("div");
						var checkboxLength = boxes.length;
						
						for( var i = 0; i < data.stories.length; i++ ) {
							var add = 1;
							var story = data.stories[i];
				
							for (var k = 0; k < checkboxLength; k++){
								var checkbox = document.getElementById("tag" + k);
								if (checkbox.checked){
									if (story.tags.indexOf( checkbox.value ) == -1 ) {
										add = 0;
									}
								}	
							}
							if( add != 0 ) {
								var bottomRow = storybody.insertRow ( 0 );
								var topRow = storybody.insertRow( 0 );
								
								topRow.className = "rowon story1";
								var topcell1 = topRow.insertCell( 0 );
								var topcell2 = topRow.insertCell( 1 );
								var topcell3 = topRow.insertCell( 2 );
								var topcell4 = topRow.insertCell( 3 );
								
								topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
								topcell2.className = "small_date";
								topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
								topcell3.className = "story_post";
								topcell3.innerHTML = story.numpost;
								topcell4.className = "small_date";
								topcell4.id = "time" + story.storyid;
								topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
								
								bottomRow.className = "rowon";
								var bottom1 = bottomRow.insertCell( 0 );
								bottom1.className = "story_preview";
								bottom1.colSpan = "4";
								bottom1.innerHTML = story.preview;
							} 
						}
					}
					
					}
				
				}
}


function advancedSearchInit(){
	var output = document.getElementById('belowsearch');
	output.style.display="none";
}


function advancedSearchStories(){
	
	var allCheckboxes = document.getElementById('tag_container');
	var boxes = allCheckboxes.getElementsByTagName("div");
	var checkboxLength = boxes.length;
	var tagindex = 0;
	
	var tagsURL = "";

	for (var i = 0; i<checkboxLength; i++){
		var checkbox = document.getElementById("tag" + i);
		
		if (checkbox.checked){
			tagsURL = tagsURL + "&tag" + tagindex + "=" + checkbox.value;
			tagindex++;
		}	
	}
	
	var keyword = document.getElementById('keywordField').value;	
	var author = document.getElementById('authorField').value;
	
	var sortBy = document.getElementById('sortby');
	var index = sortBy.selectedIndex;
	var sortMethod = sortBy.options[index].value;
	
	$.getJSON("/writeonproject/update?page=search_stories&keywords=" + keyword + "&author=" + author + "&tagIndex=" + tagindex + tagsURL + "&sortBy=" + sortMethod, function(data){
		
		if( data.error != null && data.error == "false" ) {
			
			//Display the headings for search results table
			var output = document.getElementById('belowsearch');
			output.style.display="inline";
			
			var resultstable = document.getElementById('resultstable');
			
			for (var i = resultstable.rows.length; i > 0; i--){
				resultstable.deleteRow(0);
			}
			
			
			if (data.stories.length == 0){
				var bottomRow = resultstable.insertRow ( 0 );
				bottomRow.className = "rowon";
				var bottom1 = bottomRow.insertCell( 0 );
				bottom1.className = "story_preview";
				bottom1.colSpan = "4";
				bottom1.innerHTML = "Sorry, there are no matching stories for the above criteria.";
			}
			
			if( data.stories.length > 0 ) {

				for (var i = resultstable.rows.length; i > 0; i--){
					resultstable.deleteRow(0);
				}
				
				if( data.stories.length > 0 ) {
					for( var i = 0; i < data.stories.length; i++ ) {
						
						var story = data.stories[i];
					
							var bottomRow = resultstable.insertRow ( 0 );
							var topRow = resultstable.insertRow( 0 );
							
							topRow.className = "rowon story1";
							var topcell1 = topRow.insertCell( 0 );
							var topcell2 = topRow.insertCell( 1 );
							var topcell3 = topRow.insertCell( 2 );
							var topcell4 = topRow.insertCell( 3 );
							
							topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
							topcell2.className = "small_date";
							topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
							topcell3.className = "story_post";
							topcell3.innerHTML = story.numpost;
							topcell4.className = "small_date";
							topcell4.id = "time" + story.storyid;
							topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
							
							bottomRow.className = "rowon";
							var bottom1 = bottomRow.insertCell( 0 );
							bottom1.className = "story_preview";
							bottom1.colSpan = "4";
							bottom1.innerHTML = story.preview;
						} 
				}
					
			}
				
			}
			
		}
	);
	
}